File tree Expand file tree Collapse file tree 11 files changed +931
-0
lines changed Expand file tree Collapse file tree 11 files changed +931
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minDepth (TreeNode *root) {
4
+ if (root == nullptr ) {
5
+ return 0 ;
6
+ }
7
+
8
+ if (root->left == nullptr && root->right == nullptr ) {
9
+ return 1 ;
10
+ }
11
+
12
+ int min_depth = INT_MAX;
13
+ if (root->left != nullptr ) {
14
+ min_depth = min (minDepth (root->left ), min_depth);
15
+ }
16
+ if (root->right != nullptr ) {
17
+ min_depth = min (minDepth (root->right ), min_depth);
18
+ }
19
+
20
+ return min_depth + 1 ;
21
+ }
22
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool hasPathSum (TreeNode *root, int sum) {
4
+ if (root == nullptr ) {
5
+ return false ;
6
+ }
7
+ queue<TreeNode *> que_node;
8
+ queue<int > que_val;
9
+ que_node.push (root);
10
+ que_val.push (root->val );
11
+ while (!que_node.empty ()) {
12
+ TreeNode *now = que_node.front ();
13
+ int temp = que_val.front ();
14
+ que_node.pop ();
15
+ que_val.pop ();
16
+ if (now->left == nullptr && now->right == nullptr ) {
17
+ if (temp == sum) return true ;
18
+ continue ;
19
+ }
20
+ if (now->left != nullptr ) {
21
+ que_node.push (now->left );
22
+ que_val.push (now->left ->val + temp);
23
+ }
24
+ if (now->right != nullptr ) {
25
+ que_node.push (now->right );
26
+ que_val.push (now->right ->val + temp);
27
+ }
28
+ }
29
+ return false ;
30
+ }
31
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<vector<int >> ret;
4
+ vector<int > path;
5
+
6
+ void dfs (TreeNode* root, int sum) {
7
+ if (root == nullptr ) {
8
+ return ;
9
+ }
10
+ path.emplace_back (root->val );
11
+ sum -= root->val ;
12
+ if (root->left == nullptr && root->right == nullptr && sum == 0 ) {
13
+ ret.emplace_back (path);
14
+ }
15
+ dfs (root->left , sum);
16
+ dfs (root->right , sum);
17
+ path.pop_back ();
18
+ }
19
+
20
+ vector<vector<int >> pathSum (TreeNode* root, int sum) {
21
+ dfs (root, sum);
22
+ return ret;
23
+ }
24
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public void flatten (TreeNode root ) {
3
+ List <TreeNode > list = new ArrayList <TreeNode >();
4
+ preorderTraversal (root , list );
5
+ int size = list .size ();
6
+ for (int i = 1 ; i < size ; i ++) {
7
+ TreeNode prev = list .get (i - 1 ), curr = list .get (i );
8
+ prev .left = null ;
9
+ prev .right = curr ;
10
+ }
11
+ }
12
+
13
+ public void preorderTraversal (TreeNode root , List <TreeNode > list ) {
14
+ if (root != null ) {
15
+ list .add (root );
16
+ preorderTraversal (root .left , list );
17
+ preorderTraversal (root .right , list );
18
+ }
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numDistinct (String s , String t ) {
3
+ int [][] dp = new int [t .length () + 1 ][s .length () + 1 ];
4
+ for (int j = 0 ; j < s .length () + 1 ; j ++) dp [0 ][j ] = 1 ;
5
+ for (int i = 1 ; i < t .length () + 1 ; i ++) {
6
+ for (int j = 1 ; j < s .length () + 1 ; j ++) {
7
+ if (t .charAt (i - 1 ) == s .charAt (j - 1 )) dp [i ][j ] = dp [i - 1 ][j - 1 ] + dp [i ][j - 1 ];
8
+ else dp [i ][j ] = dp [i ][j - 1 ];
9
+ }
10
+ }
11
+ return dp [t .length ()][s .length ()];
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public Node connect (Node root ) {
3
+ if (root == null ) {
4
+ return root ;
5
+ }
6
+
7
+ // 初始化队列同时将第一层节点加入队列中,即根节点
8
+ Queue <Node > queue = new LinkedList <Node >();
9
+ queue .add (root );
10
+
11
+ // 外层的 while 循环迭代的是层数
12
+ while (!queue .isEmpty ()) {
13
+
14
+ // 记录当前队列大小
15
+ int size = queue .size ();
16
+
17
+ // 遍历这一层的所有节点
18
+ for (int i = 0 ; i < size ; i ++) {
19
+
20
+ // 从队首取出元素
21
+ Node node = queue .poll ();
22
+
23
+ // 连接
24
+ if (i < size - 1 ) {
25
+ node .next = queue .peek ();
26
+ }
27
+
28
+ // 拓展下一层节点
29
+ if (node .left != null ) {
30
+ queue .add (node .left );
31
+ }
32
+ if (node .right != null ) {
33
+ queue .add (node .right );
34
+ }
35
+ }
36
+ }
37
+
38
+ // 返回根节点
39
+ return root ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ // package com.leetcode.explore.learnCard.dataStructureBinaryTree.conclusion4;
2
+
3
+ /**
4
+ * 补充节点的右侧指针,不是完美二叉树
5
+ */
6
+ public class Solution {
7
+ public Node connect (Node root ) {
8
+ if (root == null || (root .right == null && root .left == null )) {
9
+ return root ;
10
+ }
11
+ if (root .left != null && root .right != null ) {
12
+ root .left .next = root .right ;
13
+ root .right .next = getNextNoNullChild (root );
14
+ }
15
+ if (root .left == null ) {
16
+ root .right .next = getNextNoNullChild (root );
17
+ }
18
+ if (root .right == null ) {
19
+ root .left .next = getNextNoNullChild (root );
20
+ }
21
+
22
+ //这里要注意:先递归右子树,否则右子树根节点next关系没建立好,左子树到右子树子节点无法正确挂载
23
+ root .right = connect (root .right );
24
+ root .left = connect (root .left );
25
+
26
+ return root ;
27
+ }
28
+
29
+ /**
30
+ * 一路向右找到有子节点的根节点
31
+ */
32
+ private static Node getNextNoNullChild (Node root ) {
33
+ while (root .next != null ) {
34
+ if (root .next .left != null ) {
35
+ return root .next .left ;
36
+ }
37
+ if (root .next .right != null ) {
38
+ return root .next .right ;
39
+ }
40
+ root = root .next ;
41
+ }
42
+ return null ;
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<vector<int >> generate (int numRows) {
4
+ vector<vector<int >> ret (numRows);
5
+ for (int i = 0 ; i < numRows; ++i) {
6
+ ret[i].resize (i + 1 );
7
+ ret[i][0 ] = ret[i][i] = 1 ;
8
+ for (int j = 1 ; j < i; ++j) {
9
+ ret[i][j] = ret[i - 1 ][j] + ret[i - 1 ][j - 1 ];
10
+ }
11
+ }
12
+ return ret;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ class Solution1 {
2
+ public:
3
+ vector<int > getRow (int rowIndex) {
4
+ vector<vector<int >> dp (rowIndex + 1 , vector<int >(rowIndex + 1 , 0 ));
5
+ for (int i = 0 ; i <= rowIndex; i++) {
6
+ dp[i][0 ] = 1 ;
7
+ dp[i][i] = 1 ;
8
+ }
9
+
10
+ for (int i = 1 ; i <= rowIndex; i++) {
11
+ for (int j = 1 ; j < i; j++) {
12
+ dp[i][j] = dp[i - 1 ][j - 1 ] + dp[i - 1 ][j];
13
+ }
14
+ }
15
+
16
+ return dp[rowIndex];
17
+ }
18
+ };
19
+
20
+ class Solution {
21
+ public:
22
+ vector<int > getRow (int rowIndex) {
23
+ vector<int > dp (rowIndex + 1 , 1 );
24
+ for (int i = 1 ; i <= rowIndex; i++) {
25
+ for (int j = i - 1 ; j >= 1 ; j--) {
26
+ dp[j] = dp[j - 1 ] + dp[j];
27
+ }
28
+ }
29
+
30
+ return dp;
31
+ }
32
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int minimumTotal (vector<vector<int >>& triangle) {
4
+ int n = triangle.size ();
5
+ vector<vector<int >> f (n, vector<int >(n));
6
+ f[0 ][0 ] = triangle[0 ][0 ];
7
+ for (int i = 1 ; i < n; ++i) {
8
+ f[i][0 ] = f[i - 1 ][0 ] + triangle[i][0 ];
9
+ for (int j = 1 ; j < i; ++j) {
10
+ f[i][j] = min (f[i - 1 ][j - 1 ], f[i - 1 ][j]) + triangle[i][j];
11
+ }
12
+ f[i][i] = f[i - 1 ][i - 1 ] + triangle[i][i];
13
+ }
14
+ return *min_element (f[n - 1 ].begin (), f[n - 1 ].end ());
15
+ }
16
+ };
You can’t perform that action at this time.
0 commit comments