leetcode 101,104,107,108

101Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   / \
  2   2
   \   \
   3    3
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        return ismirror(root.left,root.right);
    }
    
    public boolean ismirror(TreeNode left,TreeNode right)
    {
        if(left==null&&right==null) return true;
        if(left==null||right==null) return false;
        if(left.val!=right.val) return false;
        return ismirror(left.left,right.right)&&ismirror(left.right,right.left);
    }
    
}

104Maximum Depth of Binary Tree

Example:

Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.

递归版

  public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return left>right?left+1:right+1;
    }

用栈深度优先

扫描二维码关注公众号,回复: 1751702 查看本文章

   用两个栈 stack用来深度遍历树 values存储遍历时到每个节点时的高度 max存储最大值

  public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        Stack<TreeNode> stack = new Stack<>();
        Stack<Integer>   values = new Stack<>();
        stack.push(root);
        values.push(1);
        int max=1;
        while(!stack.isEmpty())
        {
            TreeNode temp = stack.pop();
            int     val = values.pop();
            max = Math.max(max,val);
            if(temp.left!=null)
            {
                stack.push(temp.left);
                values.push(val+1);
            }
            if(temp.right!=null)
            {
                stack.push(temp.right);
                values.push(val+1);
            }
            
        }
        return max;
    }

用队列广度优先遍历

 public int maxDepth(TreeNode root) {
        if(root==null) return 0;
       int count=0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int size=0;
        TreeNode temp;
        while(!queue.isEmpty())
        {
          size=queue.size();//每一层有多少个元素
            while(size-->0)
            {
                temp=queue.poll();
                if(temp.left!=null) queue.offer(temp.left);
                if(temp.right!=null) queue.offer(temp.right);
            }
            count++;//遍历完一层高度+1
          
        }
        return count;
    }

107Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7],
  [9,20],
  [3]
]
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
     
        List<List<Integer>> res = new LinkedList<>();
          if(root==null) return res;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty())
        {
            int size = queue.size();
            List<Integer> list = new LinkedList<>();
            while(size-->0)
            {
                TreeNode p = queue.poll();
                if(p.left!=null) queue.offer(p.left);
                if(p.right!=null) queue.offer(p.right);
                list.add(p.val);   
            }
            res.add(0, list);
        }
        return res;
    }
}
方法二
  public List<List<Integer>> levelOrderBottom(TreeNode root) {
		 LinkedList<List<Integer>> res = new LinkedList<>();
		 if(root==null) return res;
		 leveladd(res,root,0); 
		 return res;      
	  }

	private void leveladd( LinkedList<List<Integer>> res, TreeNode root, int level) {
		if(root==null) return;
		if(res.size()-1<level)  res.addFirst(new LinkedList<Integer>());//如果对应层级的list不存在则新建一个
		res.get(res.size()-1-level).add(root.val);//在res中取对应层级的list存储值
		leveladd(res,root.left,level+1);
		leveladd(res,root.right,level+1);	
	}


108Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / \
   -3   9
   /   /
 -10  5

将排序数组转变为排序二叉树

数组为排序二叉树中序遍历的结果

  public TreeNode sortedArrayToBST(int[] nums) {
		return convert(nums,0,nums.length-1);
    }

	private TreeNode convert(int[] nums, int left,int right) {
	    
        if(left>right) return null;
        TreeNode head = null;
		int mid=(left+right)/2;
		head = new TreeNode(nums[mid]);
		head.left=convert(nums,left,mid-1);
		head.right=convert(nums,mid+1,right);
		return head;
	}




猜你喜欢

转载自blog.csdn.net/pipiang/article/details/80154976