Binary Tree Maximum Node

来源:互联网 发布:unity3d的web控件 编辑:程序博客网 时间:2024/05/29 14:48

Find the maximum node in a binary tree, return the node.

Given a binary tree:

     1   /   \ -5     2 / \   /  \0   3 -4  -5 

return the node with value 3.

解题思路:利用递归,比较里面的每一个

class Solution {public:    /**     * @param root the root of binary tree     * @return the max node     */             TreeNode* b = new TreeNode(-10000);        void Node(TreeNode* root)    {        if (root == NULL)        {            return;        }        if (root->val > b->val)        {            b = root;        }        Node(root->left);        Node(root->right);    }    TreeNode* maxNode(TreeNode* root) {        // Write your code here        if (root == NULL)        {            return NULL;        }        Node(root);        return b;    }};


0 0
原创粉丝点击