精华内容
下载资源
问答
  • Binary Tree

    2016-12-16 14:41:55
    Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how ...
  • BinaryTree

    2009-03-19 16:33:00
    template struct binaryTree{ paraType info; binaryTree *llink; binaryTree *rlink;} 

     

     

     template <class paraType>

    struct binaryTree

    {

        paraType info;

        binaryTree *llink;

        binaryTree *rlink;

    }

     

    展开全文
  • Binary Tree Inorder Traversal 题目描述 代码实现 Binary Tree Preorder Traversal 题目描述 代码实现 Binary Tree Postorder Traversal 题目描述 代码实现94. Binary Tree Inorder Traversal题目描述Given a ...

    94. Binary Tree Inorder Traversal

    题目描述

    Given a binary tree, return the inorder traversal of its nodes’ values.

    For example:
    Given binary tree [1,null,2,3],

       1
        \
         2
        /
       3

    return [1,3,2].

    代码实现

    /**
     * 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:
        void inorderTraversalNode(TreeNode *root, vector<int> &res) {
            if(!root) return;
    
            inorderTraversalNode(root->left, res);
            res.push_back(root->val);
            inorderTraversalNode(root->right, res);
        }
    
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> res;
            inorderTraversalNode(root, res);
            return res;
        }
    };

    修改了代码以后,没有变快:

    /**
     * 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:
        void inorderTraversalNode(TreeNode *root, vector<int> &res) {
            if(!root) return;
    
            if(root->left) inorderTraversalNode(root->left, res);
            res.push_back(root->val);
            if(root->right) inorderTraversalNode(root->right, res);
        }
    
        vector<int> inorderTraversal(TreeNode* root) {
            vector<int> res;
            inorderTraversalNode(root, res);
            return res;
        }
    };

    144. Binary Tree Preorder Traversal

    题目描述

    Given a binary tree, return the preorder traversal of its nodes’ values.

    For example:
    Given binary tree {1,#,2,3},

       1
        \
         2
        /
       3

    return [1,2,3].

    代码实现

    上面的代码换个位置就好了。

    /**
     * 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:
        void preO(TreeNode* root, vector<int> &res) {
            if(!root) return;
            res.push_back(root->val);
            preO(root->left, res);
            preO(root->right, res);
        }
        vector<int> preorderTraversal(TreeNode* root) {
            vector<int> res;
            preO(root, res);
            return res;
        }
    };

    其实可以使用栈来模拟递归的调用。

    /**
     * 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> preorderTraversal(TreeNode* root) {
            vector<int> res;
            stack<TreeNode*> stk;
            TreeNode* tp;
            if(root)  stk.push(root);
            while(!stk.empty()) {
                tp = stk.top();
                stk.pop();
                res.push_back(tp->val);
                if(tp->right) stk.push(tp->right);
                if(tp->left) stk.push(tp->left);
            }
            return res;
        }
    };

    145. Binary Tree Postorder Traversal

    题目描述

    Given a binary tree, return the postorder traversal of its nodes' values.
    
    For example:
    Given binary tree {1,#,2,3},
       1
        \
         2
        /
       3
    return [3,2,1].

    代码实现

    /**
     * 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:
        void postOrder(vector<int> &res, TreeNode* root) {
            if(!root) return;    
            if(root->left) postOrder(res, root->left);
            if(root->right) postOrder(res, root->right);
            res.push_back(root->val);
        }
    
        vector<int> postorderTraversal(TreeNode* root) {
            vector<int> res;
            postOrder(res, root);
            return res;
        }
    };
    展开全文
  • This is the fourth article on binary tree operations. For other topics on binary tree, please refer to:...Binary Tree Operations(I)Binary Tree Operations(II)Binary Tree Operations(III) - Conver

    This is the fourth article on binary tree operations. For other topics on binary tree, please refer to:

    1. Binary Tree Operations(I)
    2. Binary Tree Operations(II)
    3. Binary Tree Operations(III) - Convert a Binary Tree to Down-Right Representation

    The problem is: given a binary tree, how to determine if it is a Binary Search Tree(BST) or not? A binary search tree is a binary tree data structure which has the following properties.

    • The left subtree of a node contains only nodes with keys less than the node’s key.
    • The right subtree of a node contains only nodes with keys greater than the node’s key.
    • Both the left and right subtrees must also be binary search trees.
    - See more at: http://bo-yang.github.io/2014/10/09/is-valid-bst/#sthash.IE0JNnYd.dpuf
    展开全文
  • Balanced Binary Tree 地柜遍历

    Balanced Binary Tree

    递归遍历树


    Binary Tree Level Order Traversal I&II, Maximum Depth of Binary Tree

    树 BFS


    Plus One

    简单大数加法

    展开全文
  • Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,
  • Symmetric Tree 题目描述 代码实现 Maximum Depth of Binary Tree 题目描述 代码实现 Minimum Depth of ... Symmetric Tree题目描述Given a binary tree, check whether it is a mirror of itself (ie, symmetric a
  • 满二叉树(Full Binary Tree)、完全二叉树(Complete Binary Tree)
  • Invert Binary Tree

    2020-06-03 15:32:27
    Prompt Write a function that takes in a Binary Tree and inverts it. In other words, the function should swap every ...Each BinaryTree node has an integer value , a left child node, and a right child node
  • Binary Tree Level Order Traversal II 题目描述 代码实现 Binary Tree Level Order Traversal 题目描述 代码实现 Binary Tree Zigzag Level Order Traversal 题目描述 代码实现107. Binary Tree Level Order ...
  • Binary Tree Traversals

    2017-06-13 12:13:30
    A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which ...
  • Balance Binary Tree

    2017-03-17 17:45:56
    Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node ne
  • Balanced Binary Tree

    2017-03-01 19:11:24
    Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by
  • Binary Tree ADT

    2014-09-19 16:26:06
    int height(struct binarytree *T); void printtree(struct binarytree *T); void makeempty(struct binarytree *T); struct binarytree* find(struct binarytree *T,int x); struct binarytree* findmin(struct bin
  • Binary Tree Tilt

    2017-04-23 21:49:09
    Binary Tree Tilt Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and ...
  • 1、Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. ...
  • Diameter of Binary Tree

    2017-04-08 03:04:19
    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may ...
  • 1、Maximum Depth of Binary Tree Total Accepted: 10889 Total Submissions: 24617 My Submissions Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 74,597
精华内容 29,838
关键字:

binarytree