面试笔试杂项积累-leetcode 111-115

来源:互联网 发布:域名注册.网址是什么 编辑:程序博客网 时间:2024/04/29 16:33

111.111-Minimum Depth of Binary Tree-Difficulty: Easy

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

思路

求出二叉树的最小长度,我们知道求最大长度是用(l>r)? l+1:r+1。如你所想,最小长度是(l<r)? l+1:r+1,但有需要注意的问题。

因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0)

只有当前节点的左右子树为空时才是最后一个节点,后面的题也会出现这样的问题


/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */ //结题思路:和找最大距离不同之处在于:找最小距离要注意(l<r)? l+1:r+1 //的区别应用,因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可!public class Solution {     IList<int> list = new List<int>();    public int MinDepth(TreeNode root)    {            if (root == null)            return 0;        return getDeep(root);    }    public int getDeep(TreeNode temp)    {        int deep = 0;        if (temp != null)        {            int lDeep = getDeep(temp.left);            int rDeep = getDeep(temp.right);            if (lDeep == 0)                return rDeep + 1;            if (rDeep == 0)                return lDeep + 1;            deep = lDeep < rDeep ? lDeep + 1 : rDeep + 1;        }        return deep;    }}


112.112-Path Sum-Difficulty: Easy

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5             / \            4   8           /   / \          11  13  4         /  \      \        7    2      1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

思路

所有路径节点的和中是否有给定数值

博主根据getDeep稍微改改,遍历然后减减减,减到0并且是最后一个节点即为真。说道最后一个节点,又遇到了上面一道题的问题,只有当前节点的左右子树为空时才为是最后一个节点,切记

另外注意,存在节点可能为负数,sum也可能为负,不要手欠加判断

/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */public class Solution {    public bool HasPathSum(TreeNode root, int sum) {                if (root == null)            return false;      return getDeep(root, sum);    }    public bool getDeep(TreeNode temp, int residue)    {        if (temp != null)        {            if (temp.left == null && temp.right == null&&residue-temp.val == 0)                return true;            if (getDeep(temp.left, residue - temp.val))            {                return true;            }            if (getDeep(temp.right, residue - temp.val))            {                return true;            }        }        return false;    }    }

113.113-Path Sum II-Difficulty: Medium

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.

For example:
Given the below binary tree and sum = 22,
              5             / \            4   8           /   / \          11  13  4         /  \    / \        7    2  5   1

return

[   [5,4,11,2],   [5,8,4,5]]

思路

上一道题的进化题,求出所有节点和为sum的所有路径,运用回溯即可解决

/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */public class Solution {            IList<IList<int>> fin = new List<IList<int>>();    public IList<IList<int>> PathSum(TreeNode root, int sum) {    if (root == null)            return fin;        getDeep(root, sum, new List<int>());        return fin;    }    public void getDeep(TreeNode temp, int residue, List<int> list)    {        if (temp != null)        {            list.Add(temp.val);            if (temp.left == null && temp.right == null && residue - temp.val == 0)            { fin.Add(new List<int>(list));   list.RemoveAt(list.Count - 1); return; }            getDeep(temp.left, residue - temp.val, list);                        getDeep(temp.right, residue - temp.val, list);            list.RemoveAt(list.Count - 1);        }    }}

114.114-Flatten Binary Tree to Linked List-Difficulty: Medium

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1        / \       2   5      / \   \     3   4   6
The flattened tree should look like:
   1    \     2      \       3        \         4          \           5            \             6

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

思路

这个主要是看了hints,然后就找他说的先序遍历,,,然后就accepted

/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int x) { val = x; } * } */public class Solution { IList<int> list = new List<int>();    public void Flatten(TreeNode root)    {                if (root == null)            return;        TreeNode temp = root;        PrePrint(root);                root.left = null;        for (int i = 1; i < list.Count; i++)        {            temp.right = new TreeNode(list[i]);            temp = temp.right;        }    }    void PrePrint(TreeNode root)    {        list.Add(root.val);        if (root.left != null)            PrePrint(root.left);        if (root.right != null)            PrePrint(root.right);    }}

115.115-Flatten Binary Tree to Linked List-Difficulty: Medium

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie,"ACE" is a subsequence of "ABCDE" while "AEC" is not).

Here is an example:
S = "rabbbit", T = "rabbit"

Return 3.

思路

动态规划

动态规划题目。

以S ="rabbbit",T = "rabbit"为例):

  

参考:

http://blog.csdn.net/feliciafay/article/details/42959119

public class Solution {    public int NumDistinct(string s, string t) {                if (s.Length == 0)            return 0;                int[,] temp = new int[t.Length + 1, s.Length+1];        for (int i = 0; i < s.Length+1; i++)            temp[0, i] = 1;        for (int i = 1; i < t.Length + 1; i++)            for (int j = 1; j < s.Length+1; j++)            {                   temp[i, j] = temp[i, j - 1];                if (s[j - 1] == t[i - 1])                    temp[i, j] += temp[i-1, j - 1];                                     }        return temp[t.Length, s.Length ];    }}




























0 0
原创粉丝点击