Lintcode:二叉树的最大节点
2017-12-05 12:25
363 查看
在二叉树中寻找值最大的节点并返回。
样例
给出如下一棵二叉树:
返回值为
python:
C++:
样例
给出如下一棵二叉树:
1 / \ -5 2 / \ / \ 0 3 -4 -5
返回值为
3的节点。
python:
class Solution: """ @param: root: the root of tree @return: the max node """ def maxNode(self, root): # write your code here if root is None: return self.max(root) return self.node maxVal = -9999 node = None def max(self, root): if root is None: return if root.val >= self.maxVal: self.maxVal = root.val self.node = root self.max(root.left) self.max(root.right)
C++:
class Solution { public: /* * @param root: the root of tree * @return: the max node */ int maxx=-99999; TreeNode *pmax; void max(TreeNode *root) { if(root==NULL) return ; if(root->val>maxx) { maxx=root->val; pmax=root; } max(root->left); max(root->ri 9fd1 ght); } TreeNode * maxNode(TreeNode * root) { // write your code here if(root==NULL) return NULL; max(root); return pmax; } };
相关文章推荐
- LintCode之二叉树的最大节点
- LintCode:二叉树的最大节点632
- LintCode之632 二叉树最大节点
- Lintcode---二叉树的最大节点
- lintcode--二叉树的最大节点
- lintcode--二叉树中的最大路径和
- LintCode-二叉树的最大深度
- lintcode binary-tree-maximum-path-sum 二叉树中的最大路径和
- Lintcode-递归-97 二叉树的最大深度
- lintcode 二叉树的最大深度
- lintcode :二叉树的最大深度
- lintcode&九章算法——No.97 二叉树的最大深度 ? 待解决
- LintCode:二叉树的最大深度
- LintCode-二叉树中的最大路径和
- lintcode——二叉树的最大深度
- lintcode maximum-depth-of-binary-tree 二叉树的最大深度
- lintcode 85 在二叉树中插入节点
- Lintcode---二叉树的最大深度
- 二叉树节点间的最大距离问题
- 求二叉树节点的最大距离