LeetCode Weekly Contest 145 2問目 - Lowest Common Ancestor of Deepest Leaves [1123]

Source

LeetCode Weekly Contest 145
問題文

問題概要

省略

解法

省略

cLayversion 20190714-1)のコード [C++に変換後]

#include<bits/stdc++.h>
using namespace std;
pair<int, TreeNode*> solve(TreeNode *root){
  pair<int, TreeNode*> t1, t2;
  if(root==NULL){
    return make_pair(0, root);
  }
  t1 = solve(root->left);
  t2 = solve(root->right);
  if(t1.first > t2.first){
    t1.first++;
    return t1;
  }
  if(t1.first < t2.first){
    t2.first++;
    return t2;
  }
  return make_pair(t1.first+1, root);
}
class Solution{
  public:
  TreeNode* lcaDeepestLeaves(TreeNode* root){
    pair<int, TreeNode*> res;
    res = solve(root);
    return res.second;
  }
}
;

// cLay varsion 20190714-1

// --- original code ---
// struct TreeNode{
//   int val;
//   TreeNode *left, *right;
// };
// 
// pair<int, TreeNode*> solve(TreeNode *root){
//   pair<int, TreeNode*> t1, t2;
// 
//   if(root==NULL){
//     return make_pair(0, root);
//   }
// 
//   t1 = solve(root->left);
//   t2 = solve(root->right);
// 
//   if(t1.first > t2.first){
//     t1.first++;
//     return t1;
//   }
//   if(t1.first < t2.first){
//     t2.first++;
//     return t2;
//   }
// 
//   return make_pair(t1.first+1, root);
// }
// 
// class Solution {
// public:
//   TreeNode* lcaDeepestLeaves(TreeNode* root) {
//     pair<int, TreeNode*> res;
//     res = solve(root);
//     return res.second;
//   }
// };
// 
// {
//   // main関数を適当に処理する
// }

Current time: 2024年04月18日15時48分42秒
Last modified: 2019年07月14日13時59分56秒 (by laycrs)
Tags: Competitive_Programming_Incomplete LeetCode
トップページに戻る

Logged in as: unknown user (not login)

ログイン: