101 Symmetric Tree
给定一颗二叉树,检查是否镜像对称(围绕中心对称)
分析:递归,从根节点开始,判断左节点的左子树与右节点的右子树,左节点的右子树与右节点的左子树是否相等即可!
class Solution {
public:
bool judge(TreeNode* left, TreeNode* right){
if ((!left) && (!right)) return true;
if ((!left) || (!right)) return false;
if (left->val != right->val) return false;
return judge(left->right, right->left)&&judge(left->left, right->right);
}
bool isSymmetric(TreeNode* root) {
if (!root) return true;
return judge(root->left, root->right);
}
};
非递归的写法:中间的时候不能return true,应该continue
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (!root) return true;
queue <TreeNode*> que;
que.push(root->left);
que.push(root->right);
TreeNode* leftNode;
TreeNode* rightNode;
while(!que.empty()){
leftNode = que.front();
que.pop();
rightNode = que.front();
que.pop();
if ((!leftNode) && (!rightNode)) continue;
//return true;是不对的
if ((!leftNode) || (!rightNode)) return false;
if (leftNode->val != rightNode->val) return false;
que.push(leftNode->right);
que.push(rightNode->left);
que.push(leftNode->left);
que.push(rightNode->right);
}
return true;
}
};
116 Populating Next Right Pointers in Each Node I
分析:层序遍历的思路,注意利用next指针,使得空间复杂度控制在O(1)。
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
if (root == NULL) return;
root -> next = NULL;
TreeLinkNode *curNode = root;
while (curNode) {
TreeLinkNode *node = curNode;
if (!node -> left) return;
while (node) {
node -> left -> next = node -> right;
if(node -> next) node -> right -> next = node -> next -> left;
node = node -> next;
}
curNode = curNode -> left;
}
}
};
117 Populating Next Right Pointers in Each Node II
思路:层序遍历,O(1)空间复杂度和O(n)时间复杂度
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
// current level node
TreeLinkNode *curNode = root;
while (curNode) {
//the first node of next level
TreeLinkNode *firstNode = new TreeLinkNode(0);
TreeLinkNode *nextNode = firstNode;
//level-order traversal
while(curNode){
if (curNode -> left) {
nextNode->next = curNode->left;
nextNode = nextNode->next;
}
if (curNode -> right) {
nextNode->next = curNode->right;
nextNode = nextNode->next;
}
curNode = curNode->next;
}
//jump to the next level
curNode = firstNode->next;
delete firstNode;
}
}
};
类似的思路:
public class Solution {
//based on level order traversal
public void connect(TreeLinkNode root) {
TreeLinkNode head = null; //head of the next level
TreeLinkNode prev = null; //the leading node on the next level
TreeLinkNode cur = root; //current node of current level
while (cur != null) {
while (cur != null) { //iterate on the current level
//left child
if (cur.left != null) {
if (prev != null) {
prev.next = cur.left;
} else {
head = cur.left;
}
prev = cur.left;
}
//right child
if (cur.right != null) {
if (prev != null) {
prev.next = cur.right;
} else {
head = cur.right;
}
prev = cur.right;
}
//move to next node
cur = cur.next;
}
//move to next level
cur = head;
head = null;
prev = null;
}
}
}
129. Sum Root to Leaf Numbers
分析:深度优先遍历,累计当前结点的值。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root) {
return (root, 0);
}
int df(TreeNode* root, int sum) {
if (!root) return 0;
if (!root->left&&!root->right) return sum*10+root->val;
return df(root->left, sum*10+root->val) + df(root->right, sum*10+root->val);
}
};
199. Binary Tree Right Side View
深度遍历或者广度遍历
广度优先遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
if (!root) return res;
queue <TreeNode*> nodes;
nodes.push(root);
while (!nodes.empty()) {
res.push_back(nodes.front()->val);
int len = nodes.size();
for (int i = 0; i < len; ++i){
TreeNode* cur = nodes.front();
if (cur->right) nodes.push(cur->right);
if (cur->left) nodes.push(cur->left);
nodes.pop();
}
}
return res;
}
};
前序遍历,比较简洁
class Solution {
public:
void dfs(TreeNode* root, int lv, vector<int> &res){
if(!root) return;
if(lv>=res.size()) res.push_back(root->val);
dfs(root->right,lv+1,res);
dfs(root->left,lv+1,res);
}
vector<int> rightSideView(TreeNode* root) {
vector<int> res;
dfs(root, 0, res);
return res;
}
};
236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
求任意二叉树的最小公共结点
思路并不复杂,思路就是记录从根节点达到p和q的两条路径,显然知道了两条路径之后,不相同的上一个节点就是最低公共祖先。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> qPath;
vector<TreeNode*> pPath;
void findPath(TreeNode* node, TreeNode* p, TreeNode* q, vector<TreeNode*> & res){
if (!pPath.empty() && !qPath.empty()) return;
res.push_back(node);
if (node == p) pPath = res;
if (node == q) qPath = res;
if (node -> left) findPath(node->left, p, q, res);
if (node -> right) findPath(node->right, p, q, res);
res.pop_back(); // back_track
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || !p || !q) return NULL;
vector<TreeNode*> res;//找出两个包含这两个结点的路径
findPath(root,q,p,res);
if (pPath.empty() || qPath.empty()) return NULL;// 有一个结点没有查找到
int i = 0, j = 0;
int pLen = pPath.size(), qLen = qPath.size();
while (i < pLen && j < qLen) {
if (pPath[i] != qPath[j]) return pPath[i-1];
if (i == pLen - 1) return pPath[i];
if (j == qLen - 1) return qPath[j];
++i, ++j;
}
}
};
别人的DFS的简洁的写法,但是我质疑的一点就是,如果两个节点有一个不在这个树上的时候,这样怎么处理?
TreeNode * dfsTraverse(TreeNode * root, TreeNode * p , TreeNode * q)
{
if( root == p || root == q || root == NULL)
return root;
TreeNode * parent1 = dfsTraverse(root->left, p, q);
TreeNode * parent2 = dfsTraverse(root->right, p, q);
if( parent1 && parent2)
return root;
else
return parent1 ? parent1:parent2;
}
TreeNode * lowestCommonAncestor(TreeNode * root, TreeNode * p, TreeNode * q)
{
return dfsTraverse(root, p, q);
}