226. 翻转二叉树
问题描述:
代码:
class Solution { public TreeNode invertTree(TreeNode root) { if(root==null) return null; Queuequeue = new linkedList<>(); queue.offer(root); while(!queue.isEmpty()){ TreeNode node = queue.poll(); TreeNode new1 = new TreeNode(); new1 = node.left; node.left = node.right; node.right =new1; if(node.left!=null) queue.offer(node.left); if(node.right!=null) queue.offer(node.right); } return root; } }
思路:
层序遍历
112. 路径总和
问题描述:
代码:
class Solution { public boolean hasPathSum(TreeNode root, int targetSum) { if(root==null) return false; Queuetree = new linkedList<>(); Queue val1 = new linkedList<>(); tree.offer(root); val1.offer(root.val); while(!tree.isEmpty()){ TreeNode a = tree.poll(); int t = val1.poll(); if(a.left==null&&a.right==null){ if(t==targetSum) return true; continue; } if(a.left!=null){ tree.offer(a.left); val1.offer(a.left.val+t); } if(a.right!=null){ tree.offer(a.right); val1.offer(a.right.val+t); } } return false; } }
思路:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)