File tree Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Expand file tree Collapse file tree 3 files changed +64
-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
+ TreeNode *left;
6
+ TreeNode *right;
7
+ TreeNode(int x):val(x),left(NULL),right(NULL) {}
8
+ };
9
+ */
10
+ class Solution{
11
+ public:
12
+ int maxDepth(TreeNode* root){
13
+ if(root==nullptr) return 0;
14
+ int leftDepth=maxDepth(root->left);
15
+ int rightDepth=maxDepth(root->right);
16
+ if(leftDepth>rightDepth) return leftDepth+1;
17
+ else return rightDepth+1;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ class Solution{
2
+ public:
3
+ int lengthOfLastWorld(string s){
4
+ int slength=s.size();
5
+ int i=slength-1,length=0;
6
+ if(s.size()==0)return 0;
7
+ for(i;i>=0;i--)
8
+ {
9
+ if(s[i]!=' ')
10
+ {
11
+ break;
12
+ }
13
+ }
14
+ for(i;s[i]!=' '&&i>=0;i--)
15
+ {
16
+ length++;
17
+ }
18
+ return length;
19
+ }
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
+ TreeNode *left;
6
+ TreeNode *right;
7
+ TreeNode(int x) : val(x),left(NULL),right(NULL) {}
8
+ };
9
+ */
10
+ class Solution{
11
+ public:
12
+ bool isSymmetric(TreeNode* root){
13
+ if(root==0)
14
+ return true;
15
+ return CompareNodes(root->left,root->right);
16
+ }
17
+ public:
18
+ bool CompareNodes(TreeNode* node1,TreeNode* node2){
19
+ if(node1==0&&node2==0) return true;
20
+ if(node1==0||node2==0) return false;
21
+ if(node1->val!=node2->val) return false;
22
+ else
23
+ return CompareNodes(node1->left,node2->right)&&CompareNodes(node1->right,node2->left);
24
+ }
25
+ };
You can’t perform that action at this time.
0 commit comments