File tree Expand file tree Collapse file tree 4 files changed +119
-0
lines changed Expand file tree Collapse file tree 4 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * struct TreeNode *left;
6
+ * struct TreeNode *right;
7
+ * };
8
+ */
9
+
10
+ int max (int a , int b ) {
11
+ if (a > b ) {
12
+ return a ;
13
+ }
14
+ return b ;
15
+ }
16
+
17
+
18
+ int maxDepth (struct TreeNode * root ) {
19
+
20
+ if (root == NULL ) {
21
+ return 0 ;
22
+ }
23
+ return max (
24
+ maxDepth (root -> left ),
25
+ maxDepth (root -> right )
26
+ ) + 1 ;
27
+ }
28
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * struct TreeNode *left;
6
+ * struct TreeNode *right;
7
+ * };
8
+ */
9
+
10
+ int max (int a , int b ) {
11
+ if (a > b ) {
12
+ return a ;
13
+ }
14
+ return b ;
15
+ }
16
+
17
+
18
+ int height (struct TreeNode * root ) {
19
+ if (root == NULL ) {
20
+ return -1 ;
21
+ }
22
+ return max (
23
+ height (root -> left ),
24
+ height (root -> right )
25
+ ) + 1 ;
26
+ }
27
+
28
+
29
+ bool isBalanced (struct TreeNode * root ) {
30
+
31
+ if (root == NULL ) {
32
+ return true;
33
+ }
34
+ if (abs (height (root -> left ) - height (root -> right )) < 2 && isBalanced (root -> left ) && isBalanced (root -> right )) {
35
+ return true;
36
+ }
37
+ return false;
38
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * struct TreeNode *left;
6
+ * struct TreeNode *right;
7
+ * };
8
+ */
9
+
10
+ struct TreeNode * invertTree (struct TreeNode * root ) {
11
+
12
+ if (root == NULL ) {
13
+ return root ;
14
+ }
15
+ struct TreeNode * inverted_right = invertTree (root -> right );
16
+ struct TreeNode * inverted_left = invertTree (root -> left );
17
+ root -> right = inverted_left ;
18
+ root -> left = inverted_right ;
19
+ return root ;
20
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * struct TreeNode *left;
6
+ * struct TreeNode *right;
7
+ * };
8
+ */
9
+
10
+ int max (int a , int b ) {
11
+ if (a > b ) {
12
+ return a ;
13
+ }
14
+ return b ;
15
+ }
16
+
17
+ int recur (struct TreeNode * root , int * maxDiameter ) {
18
+ if (root == NULL ) {
19
+ return 0 ;
20
+ }
21
+ int leftHeight = recur (root -> left , maxDiameter );
22
+ int rightHeight = recur (root -> right , maxDiameter );
23
+ * maxDiameter = max (* maxDiameter , leftHeight + rightHeight );
24
+ return max (leftHeight , rightHeight ) + 1 ;
25
+ }
26
+
27
+ int diameterOfBinaryTree (struct TreeNode * root ) {
28
+ int * maxDiameter ;
29
+ int result = 0 ;
30
+ maxDiameter = & result ;
31
+ recur (root , maxDiameter );
32
+ return result ;
33
+ }
You can’t perform that action at this time.
0 commit comments