文章目录
- 102. 二叉树层序遍历
- 思路
- 递归
- 226 翻转二叉树
- 递归
- 迭代
- 101 对称二叉树
- 递归
- 总结
102. 二叉树层序遍历
给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:输入:root = [1]
输出:[[1]]
示例 3:输入:root = []
输出:[]
提示:
- 树中节点数目在范围
[0, 2000]
内 -1000 <= Node.val <= 1000
思路
- 广度优先,借助队列实现,.
- 队列的长度也就是每层的元素个数,也就是出队列的个数,即每层的元素值
/*** 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>> res = new ArrayList<>();if (root == null) {return res;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()) {int size = queue.size();List<Integer> list = new ArrayList<>();for (int i = 0; i < size; i++) {TreeNode node = queue.poll();list.add(node.val);if (node.left!= null) {queue.offer(node.left);}if (node.right!= null) {queue.offer(node.right);}}res.add(list);}return res;}
}
递归
- 递归的本质即是“栈”
- 深度优先遍历
class Solution {List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> levelOrder(TreeNode root) {level(root, 1);return res;}public void level(TreeNode root, int deepth){if(root == null) return ;//返回列表中的元素个数==层级数if(res.size() < deepth){List<Integer> item = new ArrayList<Integer>();res.add(item);}res.get(deepth-1).add(root.val);//左右节点位于同一层级deepth+1level(root.left, deepth+1);level(root.right, deepth+1);}
}
226 翻转二叉树
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
示例 2:
输入:root = [2,1,3]
输出:[2,3,1]
示例 3:输入:root = []
输出:[]
提示:树中节点数目范围在 [0, 100] 内
-100 <= Node.val <= 100
递归
- 观察翻转后的结果,实质上时每个节点左右子树的翻转,每个都翻转可联想到递归实现
/*** 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) {//终止条件 节点为nullif(root == null) return root;//递归内容,交换左右节点TreeNode temp = root.left;root.left = root.right;root.right = temp;//递归入参,每个节点invertTree(root.left);invertTree(root.right);return root;}
}
迭代
- 迭代遍历中先序遍历:根左右,入栈跟右左,交换左右节点顺序即可
class Solution {public TreeNode invertTree(TreeNode root) {if(root == null) {return null;} Stack<TreeNode> stack = new Stack<TreeNode>();stack.push(root);while(!stack.isEmpty()) {TreeNode node = stack.pop();if(node.right!= null) {stack.push(node.right);}if(node.left!= null) {stack.push(node.left);}TreeNode temp = node.left;node.left = node.right;node.right = temp;}return root;}
}
101 对称二叉树
给你一个二叉树的根节点 root
, 检查它是否轴对称。
输入:root = [1,2,2,3,4,4,3] 输出:true输入:root = [1,2,2,null,3,null,3]
输出:false
提示:树中节点数目在范围 [1, 1000] 内
-100 <= Node.val <= 100
递归
-
对称,左右子树的内测和外侧要分别相同,左子树的左侧右子树的右侧;左子树的右侧右子树的左侧
-
递归入参:内外侧子树
-
终止条件:有一个子树为空,或者值不相等
/*** 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) {return checkLeftEquelsRight(root.left, root.right);}public boolean checkLeftEquelsRight(TreeNode left, TreeNode right) {if(left ==null && right ==null){return true;}if(right!=null && left == null){return false;}if(left!=null && right==null){return false;}if(left.val != right.val) {return false;}return checkLeftEquelsRight(left.left,right.right) && checkLeftEquelsRight(left.right,right.left);}}
总结
- 二叉树的解法需要借助:栈和队列实现,
- 递归很容易也很难,三要素:终止条件、返回值、局部变量