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;
    }

用栈深度优先

   用两个栈 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;
	}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值