Lintcode 入门-632. 二叉树的最大节点
2018-01-24 12:24
190 查看
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /* * @param root: the root of tree * @return: the max node */ TreeNode * maxNode(TreeNode * root) { if(root == NULL) { return NULL; } else { int p = root->val; getMax(root,&p); TreeNode* t = new TreeNode(p); return t; } } void getMax(TreeNode * root,int * p) { if(root != NULL) { if(root->val > *p) { *p = root->val; } getMax(root->left,p); getMax(root->right,p); } } };
总结:①一种不同于由下至上最后比三定点,左最大,右最大的(三数比较)更高效的方法,将数中的每一个数沿着一种遍
历顺序与一个最大值进行比较,从而获得整个二叉树中的最大值。(实现上是用一个指针指向的int变量实现的,一开始想
用队列,栈那些,但觉得浪费,随后类比出一个整形变量而不需要更大的数据结构),②如果逻辑已经考虑到了所有情况,
但请确定你的代码和你的逻辑是吻合的,不然错误会出现的你毫无预料!
相关文章推荐
- 【入门】lintcode 632: 二叉树的最大节点
- 【LintCode 入门】632. 二叉树的最大节点
- LintCode入门练习——632. 二叉树的最大节点
- LintCode入门练习——632. 二叉树的最大节点
- LintCode-(632)二叉树的最大节点
- 【LintCode-632】二叉树的最大节点(Java实现)
- lintcode 632. 二叉树的最大节点
- LintCode 二叉树的最大节点
- 632. 二叉树的最大节点(java)
- 二叉树的最大节点-LintCode
- LintCode_632_二叉树的最大结点
- LintCode_632_二叉树的最大结点
- LintCode 第632题 二叉树的最大节点
- LintCode-二叉树的最大节点
- LintCode:二叉树的最大节点632
- [Lintcode]二叉树的最大节点
- (lintcode)第632题二叉树的最大节点
- LintCode 二叉树的最大节点
- 二叉树最大节点-lintCode
- 二叉树的最大节点-LintCode