Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf025e1f

Browse files
author
haotf
committed
二叉树遍历、深度、直径
1 parentbeb5593 commitf025e1f

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

‎104.二叉树的最大深度.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=104 lang=java
3+
*
4+
* [104] 二叉树的最大深度
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
classSolution {
24+
publicintmaxDepth(TreeNoderoot) {
25+
if (root ==null) {
26+
return0;
27+
}
28+
return1 +Math.max(maxDepth(root.left),maxDepth(root.right));
29+
}
30+
}
31+
// @lc code=end

‎144.二叉树的前序遍历.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
importjava.util.ArrayList;
2+
importjava.util.List;
3+
4+
/*
5+
* @lc app=leetcode.cn id=144 lang=java
6+
*
7+
* [144] 二叉树的前序遍历
8+
*/
9+
10+
// @lc code=start
11+
/**
12+
* Definition for a binary tree node.
13+
* public class TreeNode {
14+
* int val;
15+
* TreeNode left;
16+
* TreeNode right;
17+
* TreeNode() {}
18+
* TreeNode(int val) { this.val = val; }
19+
* TreeNode(int val, TreeNode left, TreeNode right) {
20+
* this.val = val;
21+
* this.left = left;
22+
* this.right = right;
23+
* }
24+
* }
25+
*/
26+
classSolution {
27+
publicList<Integer>preorderTraversal(TreeNoderoot) {
28+
List<Integer>list =newArrayList<Integer>();
29+
if (root ==null) {
30+
returnlist;
31+
}
32+
33+
list.add(root.val);
34+
list.addAll(preorderTraversal(root.left));
35+
list.addAll(preorderTraversal(root.right));
36+
returnlist;
37+
}
38+
}
39+
// @lc code=end

‎226.翻转二叉树.java‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* @lc app=leetcode.cn id=226 lang=java
3+
*
4+
* [226] 翻转二叉树
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
classSolution {
24+
publicTreeNodeinvertTree(TreeNoderoot) {
25+
if (root ==null)
26+
returnnull;
27+
TreeNodetmp =root.left;
28+
root.left =root.right;
29+
root.right =tmp;
30+
invertTree(root.left);
31+
invertTree(root.right);
32+
returnroot;
33+
}
34+
}
35+
// @lc code=end

‎543.二叉树的直径.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode.cn id=543 lang=java
3+
*
4+
* [543] 二叉树的直径
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode() {}
15+
* TreeNode(int val) { this.val = val; }
16+
* TreeNode(int val, TreeNode left, TreeNode right) {
17+
* this.val = val;
18+
* this.left = left;
19+
* this.right = right;
20+
* }
21+
* }
22+
*/
23+
classSolution {
24+
privateintmaxDiameter =0;
25+
26+
publicintdiameterOfBinaryTree(TreeNoderoot) {
27+
traverse(root);
28+
returnmaxDiameter;
29+
}
30+
31+
privateinttraverse(TreeNoderoot) {
32+
if (root ==null) {
33+
return0;
34+
}
35+
// 计算左右子树高度
36+
intleft =traverse(root.left);
37+
intright =traverse(root.right);
38+
// 更新最大直径
39+
maxDiameter =Math.max(left +right,maxDiameter);
40+
41+
return1 +Math.max(left,right);
42+
}
43+
}
44+
// @lc code=end

‎TreeNode.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
publicclassTreeNode {
2+
intval;
3+
TreeNodeleft;
4+
TreeNoderight;
5+
6+
TreeNode() {
7+
}
8+
9+
TreeNode(intval) {
10+
this.val =val;
11+
}
12+
13+
TreeNode(intval,TreeNodeleft,TreeNoderight) {
14+
this.val =val;
15+
this.left =left;
16+
this.right =right;
17+
}
18+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp