[LeetCode]Binary Tree Traversal & Binary Tree Maximum Path Sum

本文LeetCode(http://www.leetcode.com/onlinejudge)内的binary tree travel,包含3个:

1). Binary Tree Inorder Traversal

2). Binary Tree Level Order Traversal

3). Binary Tree Maximum Path Sum

 

1). Binary Tree Inorder Traversal

 

递归很简单;常规做法。非递归版本用stack保存,right,val,left依次压入。

 

class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> v;
        if (root == NULL) return v;
        travel(root->left, v);
        v.push_back(root->val);
        travel(root->right, v);
        return v;
    }

    void travel(TreeNode* cur, vector<int>& v) {
        if (cur == NULL) return;
        travel(cur->left, v);
        v.push_back(cur->val);
        travel(cur->right, v);
    }
}; 
#include <stack>
using namespace std;
struct A {
    TreeNode* ptr;
    int val;
    A (TreeNode* p, int v=-1) : ptr(p), val(v) {}
};
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {

        vector<int> v;
        if (root == NULL) return v;
        stack<A> q;
        q.push(A(root));

        while (!q.empty()) {
            A cur = q.top(); q.pop();
            if (cur.ptr == NULL) {
                v.push_back(cur.val);
                continue;
            }
            if (cur.ptr->right != NULL) q.push(A(cur.ptr->right));
            q.push(A(NULL, cur.ptr->val));
            if (cur.ptr->left != NULL) q.push(A(cur.ptr->left));
        }
        return v;
    }
}; 

 

 

 

2). Binary Tree Level Order Traversal

 

常规做法,队列实现

 

#include <queue>
using namespace std;

class Solution {
    public:
        vector<vector<int> > levelOrder(TreeNode *root) {
            vector<vector<int> > res;

            if (root == NULL) return res;
            queue<TreeNode*> q;
            q.push(root);
            q.push(NULL);
            TreeNode* cur;
            vector<int> v;
            while (!q.empty()) {
                cur = q.front(); q.pop();
                if (cur == NULL) {
                    res.push_back(v);
                    v.clear();
                    if (!q.empty()) q.push(NULL);
                    continue;
                }
                v.push_back(cur->val);
                if (cur->left != NULL)  q.push(cur->left);
                if (cur->right != NULL) q.push(cur->right);
            }
            return res;
        }
};

 

 

3). Binary Tree Maximum Path Sum

 

 

方法:从叶节点开始向上搞。

 

#include <queue>
using namespace std;

class Solution {
    public:
        vector<vector<int> > levelOrder(TreeNode *root) {
            vector<vector<int> > res;

            if (root == NULL) return res;
            queue<TreeNode*> q;
            q.push(root);
            q.push(NULL);
            TreeNode* cur;
            vector<int> v;
            while (!q.empty()) {
                cur = q.front(); q.pop();
                if (cur == NULL) {
                    res.push_back(v);
                    v.clear();
                    if (!q.empty()) q.push(NULL);
                    continue;
                }
                v.push_back(cur->val);
                if (cur->left != NULL)  q.push(cur->left);
                if (cur->right != NULL) q.push(cur->right);
            }

            return res;
        }
};

 

 上面的code貌似贴错了。

update@2013/09/04

 

class Solution {
public:
    int res ;
    int maxPathSum(TreeNode *root) {
        res = -111111;
        maxSum(root);
        return res;
    }

    int maxSum(TreeNode* cur) {
        if (cur == NULL) return 0;
        int left = max(maxSum(cur->left) , 0);
        int right = max(maxSum(cur->right), 0);
        int t = left + right + cur->val;
        res = (t > res ? t : res);
        t = max(left, right) + cur->val;
        return max(t, 0);
    }
};

 

 

 

你可能感兴趣的:(LeetCode)