题目很好理解,即求一棵二叉树中两个节点的公共祖先。
我的解题思路是使用DFS,求出从根节点到两个待查节点各自的路径,然后从头开始比较两个路径,最后一个相等的节点即为公共祖先节点。完整代码如下。
class Solution {
public:
//DFS代码
void findNode(TreeNode* root, TreeNode* toFind, vector &curPath...
分类:
其他好文 时间:
2015-07-12 09:39:01
阅读次数:
122
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to thedefinition of LCA on Wikipedia: ...
分类:
其他好文 时间:
2015-07-12 06:58:48
阅读次数:
88
题意:由01两个数字组成的十进制数,加起来等于n,求最少有多少个这样的数字
分析:灵活创新,思维转换;如:415=111+101+101+101+1,不用想什么十进制减法什么的
代码:
#include
using namespace std;
int main()
{
char a[10];
int i=0,hight=0;
while(cin>>a[i]) i++;
for(in...
分类:
其他好文 时间:
2015-07-12 00:22:19
阅读次数:
288
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two...
分类:
其他好文 时间:
2015-07-11 22:56:23
阅读次数:
386
Balanced Binary TreeGiven a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tre...
分类:
其他好文 时间:
2015-07-11 22:49:47
阅读次数:
110
The problem becomes more difficult once the binary tree is not perfect. The idea is still similar to use a level-order traversal. Note that we do not ...
分类:
其他好文 时间:
2015-07-11 20:02:27
阅读次数:
132
A simple application of level-order traversal. Just push the last node in each level into the result.The code is as follows. 1 class Solution { 2 publ...
分类:
其他好文 时间:
2015-07-11 20:01:41
阅读次数:
107
Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For exa...
分类:
其他好文 时间:
2015-07-11 19:55:47
阅读次数:
177
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.Ac...
分类:
其他好文 时间:
2015-07-11 18:21:07
阅读次数:
121
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.Ac...
分类:
其他好文 时间:
2015-07-11 18:03:46
阅读次数:
137