LeetCode做题总结:二叉树

LeetCode做题总结:二叉树

public class Solution15 {
	public int maxDepth(TreeNode root) {
    if(root==null)   return 0;
    else return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
	}
	public static void main(String args[]){
     Solution15 solution15=new Solution15();
     TreeNode root=new TreeNode(3);
     //TreeNode l1=new TreeNode(4);
     root.left=new TreeNode(4);
     root.right=new TreeNode(5);
     root.left.left=new TreeNode(6);
     root.left.right=new TreeNode(7);
     root.right.left=new TreeNode(8);
		System.out.println(solution15.maxDepth(root));
	}
}
public class ListNode{
	int val;
	ListNode next;
	ListNode(int x)  {val=x;}
}

 LeetCode做题总结:二叉树

LeetCode做题总结:二叉树 

public boolean isValidBST(TreeNode root) {
      if(root==null)   return true;
      return helper(root,null,null);
	}
	public boolean helper(TreeNode root,Integer max,Integer min){
    if(root==null)  return true;
    if(max!=null&&root.val>=max)   return false;
    if(min!=null&&root.val<=min)   return false;
    return helper(root.left,root.val,min)&&
			helper(root.right,max,root.val);
	}