DFS递归遍历
void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
dfs(root.right);
}
BFS使用队列遍历
void bfs(TreeNode root) {
Queue<TreeNode> queue = new ArrayDeque<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll(); // Java 的 pop 写作 poll()
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
}
利用bfs进行层次遍历
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res=new ArrayList<>();
Queue<TreeNode> queue=new ArrayDeque<>();
if(root!=null){
queue.add(root);
}
while(!queue.isEmpty()){
int n=queue.size();
List<Integer> level=new ArrayList<>();
for(int i=0;i<n;i++){
TreeNode node=queue.poll();
level.add(node.val);
if(node.left!=null) queue.add(node.left);
if(node.right!=null) queue.add(node.right);
}
res.add(level);
}
return res;
}
List<List<Integer>> res=new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
dfs(root,0);
return res;
}
public void dfs(TreeNode node,Integer deep){
if(node==null) return;
deep++;
if(res.size()<deep){
List<Integer> level=new ArrayList<>();
res.add(level);
}
res.get(deep-1).add(node.val);
dfs(node.left,deep);
dfs(node.right,deep);
}
Collections.reverse(res);
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res=new ArrayList<>();
Queue<TreeNode> queue=new LinkedList<>();
if(root!=null) queue.offer(root);
while(!queue.isEmpty()){
int n=queue.size();
while(n>0){
TreeNode node=queue.poll();
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
if(n==1) res.add(node.val);
n--;
}
}
return res;
}
public List<Double> averageOfLevels(TreeNode root) {
List<Double> res=new ArrayList<>();
Queue<TreeNode> queue=new LinkedList<>();
if(root!=null) queue.add(root);
while(!queue.isEmpty()){
int len=queue.size();
int length=len;
Double n=0.0;
while(len>0){
TreeNode node=queue.poll();
n+=node.val;
if(node.left!=null) queue.add(node.left);
if(node.right!=null) queue.add(node.right);
len--;
}
res.add(n/length);
}
return res;
}
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res=new ArrayList<>();
Queue<Node> queue=new LinkedList<>();
if(root!=null) queue.offer(root);
while(!queue.isEmpty()){
int n=queue.size();
List<Integer> level=new ArrayList<>();
while(n>0){
Node cur=queue.poll();
level.add(cur.val);
for(Node child:cur.children){
if(child!=null) queue.offer(child);
}
n--;
}
res.add(level);
}
return res;
}
public List<Integer> largestValues(TreeNode root) {
List<Integer> res=new ArrayList<>();
Queue<TreeNode> queue=new LinkedList<>();
if(root!=null) queue.offer(root);
while(!queue.isEmpty()){
int n=queue.size();
List<Integer> level=new ArrayList<>();
int max=Integer.MIN_VALUE;
while(n>0){
TreeNode node=queue.poll();
max=Math.max(max,node.val);
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
n--;
}
res.add(max);
}
return res;
}
public Node connect(Node root) {
Queue<Node> queue=new LinkedList<>();
if(root!=null) queue.offer(root);
while(!queue.isEmpty()){
int n=queue.size();
for(int i=0;i<n;i++){
Node node=queue.poll();
if(i<n-1) node.next=queue.peek();
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
}
return root;
}
public int maxDepth(TreeNode root) {
if(root==null) return 0;
int LeftDeep=maxDepth(root.left);
int RightDeep=maxDepth(root.right);
return Math.max(LeftDeep,RightDeep)+1;
}
public boolean isBalanced(TreeNode root) {
if(root==null) return true;
return Math.abs(maxDepth(root.left)-maxDepth(root.right))<2&&isBalanced(root.left)&&isBalanced(root.right);
}
private int maxDepth(TreeNode root) {
if(root==null) return 0;
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
public int minDepth(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<>();
int deep=0;
if(root!=null) queue.offer(root);
while(!queue.isEmpty()){
int n=queue.size();
deep++;
while(n-->0){
TreeNode node=queue.poll();
if(node.left==null&&node.right==null) return deep;
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
}
}
return deep;
}
Java中LinkedList类的常用方法
LinkedList类有很多方法,可以模拟栈,队列,链表功能。一、队列
1.入队 add(E e );将e插入队列末尾
2.出队删除队首 poll() ; 取出队首元素,并且在队列中删除队首。队首为空返回null
pollFirst();删除并且返回第一个元素,为空则为null
pollLast();删除并且返回最后一个元素,为空则为null
3.出队删除队首 remove(); 取出队首元素,并且在队列中删除队首。队首为空报错
removeFirst();删除并且返回第一个元素
removeLast();删除并且返回最后一个元素
4.出队不删除队首 peek(); 只返回队首元素,不删除。为空则返回null
peekFirst();返回第一个元素,为空则为null
peekLast();返回最后一个元素,为空则为null
5.出队不删除队首 element(); 只返回队首元素,不删除。为空则报错二、栈
1.入栈 push(E e) ;将e入栈。 把e插入到首位。
2.出栈 pop() ; 取出栈顶元素并且将该元素出栈,就是将栈末尾元素删除。将首位的元素删除。
3.获取栈顶 peek();三、链表
1.指定位插入 add(int index, E e); 在index下标插入e元素
2.指定位删除 remove(int index) ; 删除index下标的值
删除第一次出现的o元素 remove(Object o)
3.指定位修改 set(int index, E e) ; 将index下标的值修改为e
4.指定位查询 get(int index) ; 返回index下标的元素值
查询第一个元素getFirst()
查询最末尾元素getLast()四、其他方法
1.判空 isEmpty(); 对象为空返回true,非空返回false
2.对象长度 size(); 返回对象的长度大小
3.判断元素是否在对象中 contains(Object o); o在对象中返回true,不在返回false
4.转换成数组 toArray(); 将LinkedList对象转换成数组
5.删除所有元素 clear();将对象中的元素全部删除
6.查询元素第一次出现的下标indexOf(Object o );返回对象里第一次出现的o元素,没找到返回-1
查询某元素最后一次出现的下标lastIndexOf(Object o)
————————————————
版权声明:本文为CSDN博主「tanxinji」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/wasane/article/details/122812946
public TreeNode invertTree(TreeNode root) {
if(root==null) return null;
invertTree(root.left);
invertTree(root.right);
swap(root);
return root;
}
//dfs
void swap(TreeNode root){
TreeNode cur=root.left;
root.left=root.right;
root.right=cur;
}
public TreeNode invertTree(TreeNode root) {
Queue<TreeNode> queue=new LinkedList<>();
if(root!=null) queue.offer(root);
while(!queue.isEmpty()){
int n=queue.size();
while(n-->0){
TreeNode node=queue.poll();
if(node.left!=null) queue.offer(node.left);
if(node.right!=null) queue.offer(node.right);
swap(node);
}
}
return root;
}
void swap(TreeNode root){
TreeNode cur=root.left;
root.left=root.right;
root.right=cur;
}
public boolean isSymmetric(TreeNode root) {
return compare(root.left,root.right);
}
private boolean compare(TreeNode L,TreeNode R){
if(L==null&&R==null){
return true;
}else if(L!=null&&R!=null&&L.val==R.val){
boolean Outside=compare(L.left,R.right);
boolean Inside=compare(L.right,R.left);
return Outside&&Inside;
}else{
return false;
}
}
public boolean isSymmetric(TreeNode root) {
Queue< TreeNode> queue=new LinkedList<>();
queue.offer(root.left);
queue.offer(root.right);
while(!queue.isEmpty()){
TreeNode L=queue.poll();
TreeNode R=queue.poll();
if(L==null&&R==null) continue;
if(L== null||R==null||L.val!=R.val) return false;
queue.offer(L.left);
queue.offer(R.right);
queue.offer(L.right);
queue.offer(R.left);
}
return true;
}
ublic int countNodes(TreeNode root) {
if(root==null) return 0;
return countNodes(root.left)+countNodes(root.right)+1;
}
int n=0;
public int sumOfLeftLeaves(TreeNode root) {
if (root==null) return 0;
if(root.left!=null&&root.left.left==null&&root.left.right==null) n+=root.left.val;
sumOfLeftLeaves(root.left);
sumOfLeftLeaves(root.right);
return n;
}