Leetcode 最小二叉树深度和删除数组重复元素
2013-04-11 20:43
495 查看
Number114 最小二叉树深度
Number 80 删除数组重复元素
Number 100 same tree
Number 94 二叉树中序遍历
class Solution { public: int minDepth(TreeNode *root) { int depth=0; if(root==NULL)return 0; int left=minDepth(root->left); int right=minDepth(root->right); if(root->left==NULL&&root->right!=NULL) { depth=right+1; } else if(root->right==NULL&&root->left!=NULL) { depth=left+1; } else if(root->left==NULL&&root->right==NULL) depth=depth+1; else depth=left<right ? left+1 : right+1; return depth; } };
Number 80 删除数组重复元素
class Solution { public: int removeDuplicates(int A[], int n) { if(n==0)return 0; int i,j; int current=A[0]; int count=1; for(i=1,j=1;i<n;i++) { if(A[i]==current) { count++; if(count<=2) A[j++]=current; } if(A[i]!=current) { current=A[i]; A[j++]=current; count=1; } } return j; } };
Number 100 same tree
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSameTree(TreeNode *p, TreeNode *q) { if(p==NULL&&q==NULL) return true; if(p==NULL&&q!=NULL)return false; if(p!=NULL&&q==NULL)return false; if(p->val==q->val) { return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right); } else return false; } };
Number 94 二叉树中序遍历
class Solution { public: vector<int> inorderTraversal(TreeNode *root) { vector<int> v; TreeNode *s[100]; int top=-1; if(root==NULL)return v; TreeNode *p=root; //s[++top]=p; while(p||top!=-1) { while(p) { s[++top]=p; p=p->left; } if(top!=-1) { p=s[top]; v.push_back(p->val); top--; p=p->right; } } return v; } };
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- 每天一道LeetCode-----从有序数组中删除重复元素
- 【Leetcode】从排序数组中删除重复元素
- 每日一题之LeetCode移除元素 删除有序数组重复元素
- leetcode之删除数组中重复元素
- LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2
- LeetCode | Remove Duplicates from Sorted Array(删除有序数组的重复元素)
- 每天一道LeetCode-----有序数组循环右移n位后,寻找最小值,数组中可能包含重复元素
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- leetcode:26&80 Remove Duplicates from Sorted Array 删除数组中的重复元素
- LeetCode 分类练习(1)—— 在数组中移动指定元素、删除指定元素、删除重复元素
- Leetcode018--删除数组中重复的元素
- Leetcode刷题记——26. Remove Duplicates from Sorted Array(删除数组重复元素)
- [Leetcode] Remove duplicates from sorted array 从已排序的数组中删除重复元素
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
- LintCode(easy)删除排序数组中重复元素
- 已知有序数组求最小深度二叉树
- LeetCode 26. 从排序数组中删除重复项
- JS 对象数组的元素删除,比较对象数组,删除重复对象
- 在原数组中删除重复的元素 | Python