二叉树的所有路径
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();
paths(root,"",res);
return res;
}
public void paths(TreeNode root,String path,List<String> res){
if(root != null){
StringBuffer sb = new StringBuffer(path);
sb.append(Integer.toString(root.val));
if(root.left==null&&root.right==null){
res.add(sb.toString());
}else{
sb.append("->");
paths(root.left,sb.toString(),res);
paths(root.right,sb.toString(),res);
}
}
}
}
相同的树
class Solution {
public boolean compare(TreeNode tree1,TreeNode tree2){
if(tree1==null&&tree2==null){
return true;
}
if(tree1==null||tree2==null){
return false;
}
if(tree1.val!=tree2.val)return false;
boolean compareleft = compare(tree1.left,tree2.left);
boolean compareright = compare(tree1.right,tree2.right);
boolean isSame = compareleft&&compareright;
return isSame;
}
public boolean isSameTree(TreeNode p, TreeNode q) {
return compare(p,q);
}
}
左叶子之和
BFS搜就完事了。
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root==null) return 0;
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
int ans=0;
while(!stack.empty()){
TreeNode node = stack.pop();
if(node.left!=null){
stack.add(node.left);
if(node.left.left==null&&node.left.right==null){
ans+=node.left.val;
}
}
if(node.right!=null){
stack.add(node.right);
}
}
return ans;
}
}
找树左下角的值
使用 BFS 进行「层序遍历」,每次用当前层的首个节点来更新 ans,当 BFS 结束后,ans 存储的是最后一层最靠左的节点。
class Solution {
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> d = new ArrayDeque<>();
d.addLast(root);
int ans = 0;
while (!d.isEmpty()) {
int sz = d.size();
ans = d.peek().val;
while (sz-- > 0) {
TreeNode poll = d.pollFirst();
if (poll.left != null) d.addLast(poll.left);
if (poll.right != null) d.addLast(poll.right);
}
}
return ans;
}
}
从中序与后序遍历序列构造二叉树
HashMap memo 需要一个哈希表来保存中序遍历序列中,元素和索引的位置关系。因为从后序序列中拿到根节点后,要在中序序列中查找对应的位置,从而将数组分为左子树和右子树;
postIndex:当前子树根节点在中序遍历数组中的索引位置;
left,right:当前子树在中序遍历数组的起始位置和结束位置标记;
class Solution {
HashMap<Integer,Integer> inorderArrayMap = new HashMap<>();
int[] post;
public TreeNode buildTree(int[] inorder, int[] postorder) {
for(int i = 0;i < inorder.length; i++) {
inorderArrayMap.put(inorder[i], i);//妙啊!将节点值及索引全部记录在哈希表中
}
post = postorder;
TreeNode root = buildTree(0, inorder.length - 1, 0, post.length - 1);
return root;
}
public TreeNode buildTree(int inorderStart, int inorderEnd, int postorderStart, int postorderEnd) {
if(inorderEnd < inorderStart || postorderEnd < postorderStart) return null;
int root = post[postorderEnd];//根据后序遍历结果,取得根节点
int rootIndexInInorderArray = inorderArrayMap.get(root);//获取对应的索引
TreeNode node = new TreeNode(root);//创建该节点
node.left = buildTree(inorderStart, rootIndexInInorderArray - 1, postorderStart, postorderStart + rootIndexInInorderArray - inorderStart - 1);
node.right = buildTree(rootIndexInInorderArray + 1, inorderEnd, postorderStart + rootIndexInInorderArray - inorderStart, postorderEnd - 1);
return node;//注意!返回的是新建的node!
}
}
最大二叉树
按题目递归就可以了
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return build(nums,0,nums.length-1);
}
public TreeNode build(int[] nums,int l,int r){
if(l>r)return null;
int idx = l;
for(int i = l;i<=r;i++){
if(nums[i]>nums[idx])idx=i;
}
TreeNode root = new TreeNode(nums[idx]);
root.left=build(nums,l,idx-1);
root.right=build(nums,idx+1,r);
return root;
}
}