day15 :二叉树 part02

102.二叉树的层序遍历

力扣题目链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
     public List<List<Integer>> levelOrder(TreeNode root) {
	        List<List<Integer>> list=new ArrayList<>();
	        if(root==null)return list;
	        Queue<TreeNode> tree=new LinkedList<>();
	        tree.add(root);
	        while(!tree.isEmpty()) {
	        	int size=tree.size();
	        	List<Integer> num=new ArrayList<>();
	        	for(;size>0;size--) {
	        		TreeNode fath=tree.poll();
	        		num.add(fath.val);
	        		if(fath.left!=null)tree.add(fath.left);
	        		if(fath.right!=null)tree.add(fath.right);
	        	}
	        	list.add(num);
	        }
	        return list;
	    }
}

226.翻转二叉树

力扣题目链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)return root;
		 Queue<TreeNode> tree=new LinkedList<>();
	        tree.add(root);
	        while(!tree.isEmpty()) {
	        	List<Integer> num=new ArrayList<>();
	        		TreeNode fath=tree.poll();
	        		TreeNode s=fath.left;
	        		fath.left=fath.right;
	        		fath.right=s;
	        		if(fath.left!=null)tree.add(fath.left);
	        		if(fath.right!=null)tree.add(fath.right);	        	
	        }
	        return root;
	    }
}

101. 对称二叉树

力扣题目链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    	 public boolean isSymmetric(TreeNode root) {
		 if(root==null)return true;
			return isSym(root.left,root.right);
	    }
	 public boolean isSym(TreeNode root1,TreeNode root2) {		 
		 if(root1==null&&root2==null)return true;
		 else if(root1==null||root2==null)return false;
         if(root1.val!=root2.val)return false;
		 return isSym(root1.left,root2.right)&&isSym(root1.right,root2.left);
	 }
}

记录

又学了一天,已经过了四分之一了,目前感觉良好,希望学完自己能蜕变一下,开学的比赛也不求什么名次至少能让会做的题目多一点,加油!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值