二叉树——leetcode101/leetcode102/leetcode110/leetcode111/leetcode112/leetcode257
2016-12-20 10:52
381 查看
101. Symmetric Tree
问题描述:
Total Accepted: 145790Total Submissions: 397211
Difficulty: Easy
Contributors: Admin
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
Note:
Bonus points if you could solve it both recursively and iteratively.
解析:
判断一个二叉树是否是镜像的。使用栈处理数据,注意左右节点的入栈顺序。public class Solution { public boolean isSymmetric(TreeNode root) { if(root==null) return true; Stack<TreeNode> stack=new Stack<TreeNode>(); stack.push(root.left); stack.push(root.right); while(!stack.isEmpty()){ TreeNode node1=stack.pop(); TreeNode node2=stack.pop(); if(node1==null && node2==null) continue; if(node1==null || node2==null || node1.val!=node2.val){ return false; } stack.push(node1.left); stack.push(node2.right); stack.push(node1.right); stack.push(node2.left); } return true; } }
102. Binary Tree Level Order Traversal
问题描述:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:
Given binary tree
[3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
解析:
层序遍历二叉树,每一层节点的值分别放在一个list中,所有list放在一个大list中。思想: * 深度优先搜索,按层遍历,每层的元素分别放在一个list中,最后将多有list汇总到一个大list中。 * 核心,使用队列; * 第一层先入队列,在遍历第一层元素的同时,将第二层入队列,第一层并出队列,第一层出去完之后,只剩下第二层数据了。 * 第二层开始遍历,第二层遍历的过程中,将第三层入队列。。。依次类推,直至最后一层入队列,遍历完最后一层,队列就空了。循环结束。 public static List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> queue=new LinkedList<TreeNode>(); List<List<Integer>> list=new LinkedList<List<Integer>>(); if(root==null) return list; queue.offer(root); while(!queue.isEmpty()){ int len=queue.size(); List<Integer> subList=new LinkedList<Integer>(); for(int i=0;i<len;i++){ if(queue.peek().left!=null) queue.offer(queue.peek().left); if(queue.peek().right!=null) queue.offer(queue.peek().right); subList.add(queue.poll().val); } list.add(subList); } return list; }
110. Balanced Binary Tree
问题描述:
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofevery node never differ by more than 1.
解析:
public static boolean isBalanced(TreeNode root) { if(root==null) return true; if(Math.abs(height(root.left)-height(root.right))<=1){ return (isBalanced(root.left)&&(isBalanced(root.right))); } return false; } private static int height(TreeNode root) { // TODO Auto-generated method stub if(root==null){ return 0; } int left=height(root.left); int right=height(root.right); return (Math.max(left, right)+1); }
111. Minimum Depth of Binary Tree
问题描述:
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
解析:
public int minDepth(TreeNode root) { if(root==null) return 0; if(root.left==null) return minDepth(root.right)+1; if(root.right==null) return minDepth(root.left)+1; return Math.min(minDepth(root.left), minDepth(root.right))+1; }
112. Path Sum
问题描述:
Total Accepted: 136461Total Submissions: 415931
Difficulty: Easy
Contributors: Admin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and
sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path
5->4->11->2which sum is 22.
解析:
public boolean hasPathSum(TreeNode root, int sum) { if(root==null) return false; if(root.left==null && root.right==null && sum-root.val==0) return true; return hasPathSum(root.left,sum-root.val) || hasPathSum(root.right,sum-root.val); }
257. Binary Tree Paths
问题描述:
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree:
1 / \ 2 3 \ 5
All root-to-leaf paths are:
["1->2->5", "1->3"]
解析:
找到二叉树中根节点到叶子节点的所有路径。public List<String> binaryTreePaths(TreeNode root) { List<String> list=new ArrayList<String>(); if(root!=null) searchBT(root,"",list); return list; } private static void searchBT(TreeNode root, String path, List<String> list) { // TODO Auto-generated method stub if(root.left==null && root.right==null) list.add(path+root.val); if(root.left!=null) searchBT(root.left, path+root.val+"->", list); if(root.right!=null) searchBT(root.right, path+root.val+"->", list); }
相关文章推荐
- 【LeetCode101-110】二叉树对称及存储,前序中序遍历生成二叉树,中序后序生成二叉树,数组转化为AVL平衡树,判断二叉树是否平衡
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- 二叉树与分治法-Leetcode-257
- 【LeetCode111-120】很多道二叉树的问题(左右顺序很重要),一道hard DP题,两道杨辉三角以及一道DP三角
- leetcode 111-二叉树最小深度
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- LeetCode-111-Minimum Depth of Binary Tree(二叉树的最短路径)
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- Leetcode 257 Binary Tree Paths 二叉树 DFS
- Leetcode 110 Balanced Binary Tree 二叉树
- Leetcode 101. Symmetric Tree 判断二叉树是否对称,注意写在两个方法中,递归时注意空的判断
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- Leetcode 110 判断一颗二叉树是否是平衡二叉树
- 面试题19:二叉树的镜像(Leetcode-101)
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- leetcode_111题——Minimum Depth of Binary Tree(二叉树,队列)
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
- 【Leetcode】257 Binary Tree Paths 二叉树的路径
- 【LeetCode 111_二叉树_遍历】Minimum Depth of Binary Tree
- Leetcode 101 Symmetric Tree 二叉树