And a binary value for a path; a first character position only appears once; binary tree depth; Analyzing is not a balanced binary tree

Input nodes and a binary tree with an integer, the binary print values ​​of the nodes and the paths to all the input integers. Forming a path to the path definition begins from the root node of the tree down to the leaf node has been traversed nodes. (Note: the return value in the list, a large array Array front)

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    void path(TreeNode* tree,int sum)
    {
        if(tree == nullptr)
            return;
        vec2.push_back(tree->val);
        if(tree->left == nullptr && tree->right == nullptr && sum == tree->val)
            vec1.push_back(vec2);
        else
        {
            if(tree->left)
                path(tree->left,sum - tree->val);
            if(tree->right)
                path(tree->right,sum - tree->val);
        }
        vec2.pop_back();   //数组压入二维数组之后,就会被一次一次的尾删在递归中回到双亲结点开始下一次的遍历
    }
    vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
        path(root,expectNumber);
        return vec1;
    }
private:
    vector<vector<int>> vec1;
    vector<int> vec2;
};

In one string (0 <= length of the string <= 10000, all of the alphabet) find a first character appears only once, and returns to its position, or -1 if not (case-sensitive).

class Solution {
public:
    int FirstNotRepeatingChar(string str) {
        int size = str.size();
        vector<int> vec;
        vec.resize(256); 
        for(int i = 0; i < size; ++i)
        {
            vec[str[i]]++;
        }
        for(int i = 0; i < size;++i)
        {
            if(vec[str[i]] == 1)
                return i;
        }
        return -1;
    }
};

Input binary tree, find the depth of the tree. Forming a path tree from the root node to the leaf node sequentially passes (including the root, leaf nodes), the depth of the length of the longest path in the tree.

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
class Solution {
public:
    int TreeDepth(TreeNode* pRoot)
    {
        if(pRoot == nullptr)
            return 0;
        int left = TreeDepth(pRoot->left);
        int right = TreeDepth(pRoot->right);
        return left > right ? left + 1 : right + 1;
    }
};

Input binary tree, the binary tree is determined whether a balanced binary tree.

class Solution {
public:
    int func(TreeNode* tree)
    {
        if(tree == nullptr)
            return 0;
        int left = func(tree->left);
        if(left == -1)
            return -1;
        int right = func(tree->right);
        if(right == -1)
            return -1;
        return abs(left - right) > 1 ? -1 :( 1 + ((left > right) ? left : right));
    }
    
    
    bool IsBalanced_Solution(TreeNode* pRoot) {
        if(pRoot == nullptr)
            return true;
        return func(pRoot) != -1;
    }
};
Published 230 original articles · won praise 28 · views 9305

Guess you like

Origin blog.csdn.net/weixin_43767691/article/details/103792664