跳过正文
  1. Posts/

[257] Binary Tree Paths

·1 分钟·
目录

题目:257. Binary Tree Paths
#

Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 /
2 3
5 All root-to-leaf paths are: [“1->2->5”, “1->3”]

题意:
#

难度不大,考察树的遍历

解法:
#

C++版本实现方法:


	/**
		 * 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<string> binaryTreePaths(TreeNode* root) 
			{
				vector<string> results;
				visitTreeNode(root, "", results);
				return results;
			}

		private:
			void visitTreeNode(TreeNode* node, string path, vector<string>& result)
			{
			    if(node == nullptr) return;
				if (!path.empty())
				{
					path += "->";
				}
				path += int2Str(node->val);

				if (node->left == nullptr && node->right == nullptr)
				{
					result.push_back(path);
				}
				else
				{
					if (node->left != nullptr)
					{
						visitTreeNode(node->left, path, result);
					}
					if (node->right != nullptr)
					{
						visitTreeNode(node->right, path, result);
					}
				}
			}

			std::string int2Str(int nValue)
			{
				ostringstream ss;
				ss << nValue;
				return ss.str();
			}
		}

相关文章

[018] Length Of Last Word

·1 分钟
题目: # Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space characters only. For example, Given s = “Hello World”, return 5.

[151] Reverse Words in a String

·1 分钟
题目:Reverse Words in a String # Given an input string s, reverse the string word by word. For example, given s = “the sky is blue”, return “blue is sky the”.

Two Sum

·3 分钟
本文主要包括 leetCode 题集里的两个题目,Two Sum1 和 Two Sum2 题目1: 1. Two Sum 1 # Given an array of integers, find two numbers such that they add up to a specific target number.

[152] Maximum Product Subarray

·1 分钟
题目: # Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6.

iOS 远端推送部署详解

·10 分钟
最近几天被iOS的推送部署给搞懵了,现在特地整理下和大家进行分享。 iOS远端推送机制 # APNS,全称为Apple Push Notification service,是苹果通知推送服务中最重要的一环。它是苹果通知推送服务器,为所有iOS设备以及OS X设备提供强大并且可靠的推送通知服务。每个注册通知服务的设备都会和该服务器进行长连接,从而实时获取推送通知。即使当前APP不在运行状态,当通知到达的时候也会有提示发生,最常见的就是短信服务。