heylinart

导航

Navie level questions

1. Binary Tree Maximum Node

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

 1 public class MaximumNode{
 2       public TreeNode maxNode(TreeNode root){
 3                  if(root == null) return root;
 4                  TreeNode left = maxNode(root.left);
 5                  TreeNode right = maxNode(root.right);
 6                  return max(root,max(left,right));            
 7       }          
 8       public TreeNode max(TreeNode node,TreeNode anotherNode){
 9                  if(node == null) return anotherNode;   
10                  if(anotherNode == null) return node;
11                  if(node.val > anotherNode.val) return node;
12                  return anotherNode;
13       } 
14 }                

 

posted on 2017-09-12 10:14  heylinart  阅读(126)  评论(0编辑  收藏  举报