lintcode 二叉树遍历

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /*
     * @param root: the root of tree
     * @return: the max node
     */
    public TreeNode maxNode(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode max=root;
        return find(root,max);


    }
    
    private TreeNode find(TreeNode root,TreeNode max){
        if(root.val>max.val){
            max=root;
        }
        if(root.left!=null){
            max=find(root.left,max);
        }
        if(root.right!=null){
        max=find(root.right,max);
        }
        return max;
    }
    
}

你可能感兴趣的:(lintcode)