Skip to content

Commit 66b8f89

Browse files
committed
add leetcodea: offer-34-36-54
1 parent 066163a commit 66b8f89

4 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package offer;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-07-07-17:45
9+
* @Function Description :34. 二叉树中和为某一值的路径
10+
*/
11+
public class _34ThePathOfBinaryTreeNeutralityIsACertainValue {
12+
/**
13+
* Definition for a binary tree node.
14+
* public class TreeNode {
15+
* int val;
16+
* TreeNode left;
17+
* TreeNode right;
18+
* TreeNode() {}
19+
* TreeNode(int val) { this.val = val; }
20+
* TreeNode(int val, TreeNode left, TreeNode right) {
21+
* this.val = val;
22+
* this.left = left;
23+
* this.right = right;
24+
* }
25+
* }
26+
*/
27+
class Solution {
28+
// 总方案
29+
public LinkedList<List<Integer>> res = new LinkedList<>();
30+
// 记录当前搜索的一种方案
31+
public LinkedList<Integer> path = new LinkedList<>();
32+
33+
public List<List<Integer>> pathSum(TreeNode root, int target) {
34+
dfs(root , target);
35+
return res;
36+
37+
}
38+
39+
// 先序遍历
40+
public void dfs(TreeNode root , int target ){
41+
if( root == null ) return ;
42+
path.add( root.val );
43+
target -=root.val;
44+
if( target == 0 && root.left == null && root.right == null ){
45+
res.add( new LinkedList( path ) );
46+
}
47+
dfs( root.left , target );
48+
dfs( root.right , target );
49+
path.removeLast();
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-07-18:24
6+
* @Function Description :36.二叉搜索树与双向链表
7+
*/
8+
9+
// Definition for a Node.
10+
class Node1 {
11+
public int val;
12+
public Node1 left;
13+
public Node1 right;
14+
15+
public Node1() {}
16+
17+
public Node1(int _val) {
18+
val = _val;
19+
}
20+
21+
public Node1(int _val,Node1 _left,Node1 _right) {
22+
val = _val;
23+
left = _left;
24+
right = _right;
25+
}
26+
};
27+
28+
29+
public class _36TheBinarySearchTreeAndTwoWayLinkedList {
30+
31+
class Solution {
32+
// pre上一个结点,left变成指向上一个结点的指针,right是下一个
33+
public Node1 pre , head;
34+
public Node1 treeToDoublyList(Node1 root) {
35+
if( root == null ) return null;
36+
dfs(root);
37+
head.left = pre;
38+
pre.right = head;
39+
return head;
40+
}
41+
42+
// 中序
43+
public void dfs( Node1 cur ){
44+
if( cur == null ) return ;
45+
dfs( cur.left );
46+
// 前一个结点不空,right指针指向当前
47+
if( pre != null ) pre.right = cur;
48+
// 前一个结点空,找到头结点或者
49+
else head = cur;
50+
// 当前结点left指向上一个
51+
cur.left = pre;
52+
// pre移到当前结点
53+
pre = cur;
54+
dfs( cur.right );
55+
}
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-07-18:41
6+
* @Function Description :54.二叉搜索树的第k大结点
7+
*/
8+
public class _54TheKNodeOfTheBinarySearchTree {
9+
/**
10+
* Definition for a binary tree node.
11+
* public class TreeNode {
12+
* int val;
13+
* TreeNode left;
14+
* TreeNode right;
15+
* TreeNode(int x) { val = x; }
16+
* }
17+
*/
18+
class Solution {
19+
public int res , k ;
20+
public int kthLargest(TreeNode root, int k) {
21+
this.k = k;
22+
dfs( root );
23+
return res;
24+
25+
}
26+
27+
// 中序遍历的倒序,右根左
28+
public void dfs( TreeNode root ){
29+
if( root == null ) return ;
30+
dfs( root.right );
31+
if( k == 0 ) return ;
32+
if( --k == 0 ) res = root.val;
33+
34+
dfs( root.left );
35+
36+
}
37+
}
38+
}
Binary file not shown.

0 commit comments

Comments
 (0)