中國網(wǎng)頁設(shè)計師網(wǎng)站代運(yùn)營靠譜嗎
目錄
模版
先序遍歷
中序遍歷
后序遍歷
力扣原題 相同的二叉樹
力扣原題 翻轉(zhuǎn)二叉樹
遍歷樹的層數(shù)
題目
靜態(tài)變量
核心邏輯
模版
// 二叉樹public static class Node{public int value;public Node left;public Node right;public Node(int v) {value=v;}}
先序遍歷
根節(jié)點(diǎn) 左孩子節(jié)點(diǎn) 右孩子節(jié)點(diǎn)
中序遍歷
左孩子節(jié)點(diǎn) 根節(jié)點(diǎn) 右孩子節(jié)點(diǎn)
后序遍歷
左孩子節(jié)點(diǎn) 右孩子節(jié)點(diǎn) 根節(jié)點(diǎn)
力扣原題 相同的二叉樹
100. 相同的樹 - 力扣(LeetCode)
/*** 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 isSameTree(TreeNode p, TreeNode q) {if(p==null && q!=null){return false;}if(p!=null && q==null){return false;}if(p==null && q==null){return true;}// 都不為空return p.val==q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);}
}
力扣原題 翻轉(zhuǎn)二叉樹
/*** 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 flipTree(TreeNode root) {if(root == null) return null;TreeNode tmp = root.left;root.left = flipTree(root.right);root.right = flipTree(tmp);return root;}
}
遍歷樹的層數(shù)
題目
靜態(tài)變量
核心邏輯
import java.util.*;
import java.math.*;
import java.time.*;
import java.io.*;
import java.util.regex.*;// Eclipse IDE 2020 08
// OpenJDK 1.8
// 下方網(wǎng)頁是個人主頁
// http://gczdy.cn/*.-'''-.
_______ _______ ' _ \
\ ___ `'. \ ___ `'. / /` '. \ ' |--.\ \ ' |--.\ \ . | \ ' | | \ ' | | \ ' | ' | ' | | | '| | | ' \ \ / / | | | || | | | _ _`. ` ..' / | | ' .'| | ' .'| ' / | '-...-'` | |___.' /' | |___.' /'.' | .' |
/_______.'/ /_______.'/ / | / |
\_______|/ \_______|/ | `'. | ' .'| '/ `-' `--'
*//*** * @Author Dduo* @Date 2025-01-10*/
public class Main {// 普通流
// static Scanner sc = new Scanner(System.in);// 數(shù)據(jù)流快讀模板(類似于C++的IOS)static Read sc=new Read();// 時間類 用來測試運(yùn)行時間static Instant START=Instant.now();static long MOD = (long) (1e9 + 7);static int[] dx = {0, 0, 1, -1, 1, -1, 1, -1};static int[] dy = {1, -1, 0, 0, -1, -1, 1, 1};private static final int[] DIRECTIONS = {-1, 0, 1, 0, -1};/**** @param args* @return* @throws Exception */public static void main(String[] args) throws Exception {int t = 1;
// t = sc.nextInt();// 預(yù)處理preconditioning();while (t-- > 0) {solve();}// sc.close();
// dduoln("運(yùn)行時間 : "+Duration.between(START,Instant.now()).toMillis()+"ms");return;}// 輸出流 static <T> void dduoln(T t) {System.out.println(t);}static <T> void dduo(T t) {System.out.print(t);}// 預(yù)處理static void preconditioning() {}// 數(shù)據(jù)結(jié)構(gòu)模板 二叉樹 by Dduostatic class Node{public int value;public Node left;public Node right;public Node() {}public Node(int v) {value=v;}}// 靜態(tài)變量static Node[] a = new Node[1000010];static int cnt=0;// 核心代碼邏輯static void solve() throws Exception { // 構(gòu)造二叉樹int n=sc.nextInt();for(int i=1;i<=n;i++) {a[i]=new Node(i);int l=sc.nextInt();int r=sc.nextInt();if (l != 0) {a[i].left = new Node(l);}if (r != 0) {a[i].right = new Node(r);}}dfs(a[1],1);dduoln(cnt);}static void dfs(Node node,int max){
// System.out.println("Visiting Node " + node.value + " at depth " + max);// 判斷當(dāng)前節(jié)點(diǎn)是否是葉子節(jié)點(diǎn)(即左右子節(jié)點(diǎn)都為null)if (node.left == null && node.right == null) {cnt = Math.max(cnt, max);return; }// 遍歷左子樹if (node.left != null) {dfs(a[node.left.value], max + 1);}// 遍歷右子樹if (node.right != null) {dfs(a[node.right.value], max + 1);}}// 快速冪模版 by Dduostatic long pow(long a, long b) {if (b == 0) return 1;if (b == 1) return a;try {long result = 1;while (b > 0) {if ((b & 1) == 1) {if (result > Long.MAX_VALUE / a) return Long.MAX_VALUE;result *= a;}b >>= 1;if (b > 0) {if (a > Long.MAX_VALUE / a) return Long.MAX_VALUE;a *= a;}}return result;} catch (Exception e) {return Long.MAX_VALUE;}}
}// 數(shù)據(jù)結(jié)構(gòu)模版 并查集 by Dduo
class UnionFind {private int[] parent;private int[] size;// 初始化并查集public UnionFind(int n) {parent = new int[n + 1]; // 因?yàn)榫幪枏?1 到 N,所以數(shù)組大小是 N+1size = new int[n + 1];for (int i = 1; i <= n; i++) {parent[i] = i; // 每個元素的父節(jié)點(diǎn)初始為自己size[i] = 1; // 每個元素的初始大小為 1}}// 查找元素 x 所在的集合,帶路徑壓縮優(yōu)化public int find(int x) {if (parent[x] != x) {parent[x] = find(parent[x]); // 路徑壓縮}return parent[x];}// 合并兩個集合 帶按秩合并優(yōu)化public void union(int x, int y) {int rootX = find(x);int rootY = find(y);if (rootX != rootY) {// 按秩合并 較小的樹合并到較大的樹上if (size[rootX] < size[rootY]) {parent[rootX] = rootY;size[rootY] += size[rootX];} else {parent[rootY] = rootX;size[rootX] += size[rootY];}}}// 判斷 x 和 y 是否在同一個集合中public boolean connected(int x, int y) {return find(x) == find(y);}
}// 數(shù)據(jù)流快讀模板(類似于C++的IOS) by Dduo
class Read{BufferedReader bf;StringTokenizer st;BufferedWriter bw;public Read(){bf=new BufferedReader(new InputStreamReader(System.in));st=new StringTokenizer("");bw=new BufferedWriter(new OutputStreamWriter(System.out));}
// 以下為輸入部分:public String nextLine() throws IOException{return bf.readLine();}public String next() throws IOException{while(!st.hasMoreTokens()){st=new StringTokenizer(bf.readLine());}return st.nextToken();}public char nextChar() throws IOException{//確定下一個token只有一個字符的時候再用return next().charAt(0);}public int nextInt() throws IOException{return Integer.parseInt(next());}public long nextLong() throws IOException{return Long.parseLong(next());}public double nextDouble() throws IOException{return Double.parseDouble(next());}public float nextFloat() throws IOException{return Float.parseFloat(next());}public byte nextByte() throws IOException{return Byte.parseByte(next());}public short nextShort() throws IOException{return Short.parseShort(next());}public BigInteger nextBigInteger() throws IOException{return new BigInteger(next());}public BigDecimal nextDecimal() throws IOException{return new BigDecimal(next());}
// 以下為輸出部分:public void println(int a) throws IOException{print(a);println();return;}public void print(int a) throws IOException{bw.write(String.valueOf(a));return;}public void println(String a) throws IOException{print(a);println();return;}public void print(String a) throws IOException{bw.write(a);return;}public void println(long a) throws IOException{print(a);println();return;}public void print(long a) throws IOException{bw.write(String.valueOf(a));return;}public void println(double a) throws IOException{print(a);println();return;}public void print(double a) throws IOException{bw.write(String.valueOf(a));return;}public void print(BigInteger a) throws IOException{bw.write(a.toString());return;}public void print(char a) throws IOException{bw.write(String.valueOf(a));return;}public void println(char a) throws IOException{print(a);println();return;}public void println() throws IOException{bw.newLine();return;}
// 其他調(diào)試命令:public void flush() throws IOException{
// 交互題分組調(diào)試,或者提前退出的情況下可以先運(yùn)行此語句再推出bw.flush();return;}public boolean hasNext() throws IOException{
// 本地普通IDE難以使用這個方法調(diào)試,需要按照數(shù)據(jù)組flush,刷新語句:
// sc.flush()
// 調(diào)試完可刪去return bf.ready();}
}